package rexsee.script;

public class BlockFor extends AbstractBlock {

	private final AbstractBlock start;
	private final AbstractBlock condition;
	private final AbstractBlock step;
	private final AbstractBlock body;
	public boolean breakNow = false;

	public BlockFor(AbstractBlock parent, AbstractBlock start, AbstractBlock condition, AbstractBlock step, AbstractBlock body) {
		super(parent, "");
		this.start = start;
		this.condition = condition;
		this.step = step;
		this.body = body;
	}

	@Override
	public int getChildCount(int index) {
		return 0;
	}

	@Override
	public AbstractBlock getChild(int index) {
		return null;
	}

	@Override
	public Object run() {
		start.run();
		if (condition == null || body == null) return null;
		Object rtn = condition.run();
		boolean fail = rtn instanceof Boolean && ((Boolean) rtn) == false;
		while (!fail) {
			body.run();
			if (breakNow) break;
			rtn = step.run();
			fail = rtn instanceof Boolean && ((Boolean) rtn) == false;
		}
		return null;
	}

}
